import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import Home from "../views/home/index.vue"

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        redirect: '/product'
    },
    {
        path: '',
        component: Home,
        redirect: '/product',
        children: [
            {
                path: 'product',
                name: 'Product',
                component: () => import('../views/product/index.vue'),
                meta: {
                    title: '首页',
                    login: false
                }
            },
            {
                path: 'tech',
                name: 'Tech',
                component: () => import('../views/techList/index.vue'),
                meta: {
                    title: '理疗师',
                    login: false
                }
            },
            {
                path: 'order',
                name: 'Order',
                component: () => import('../views/order/index.vue'),
                meta: {
                    title: '订单列表',
                    login: true
                }
            },
            {
                path: 'my',
                name: 'My',
                component: () => import('../views/my/index.vue'),
                meta: {
                    title: '我的',
                    login: false
                }
            }
        ]
    },
    {
        path: '/product/datail',
        name: 'ProductDetail',
        component: () => import('../views/productDetail/index.vue'),
        meta: {
            title: '项目详情',
            login: false
        }
    },
    {
        path: '/product/edit',
        name: 'ProductEdit',
        component: () => import('../views/productEdit/index.vue'),
        meta: {
            title: '改项目',
            login: true
        }
    },
    {
        path: '/product/add',
        name: 'ProductAdd',
        component: () => import('../views/productAdd/index.vue'),
        meta: {
            title: '加钟',
            login: true
        }
    },
    {
        path: '/select/tech',
        name: 'SelectTech',
        component: () => import('../views/selectTech/index.vue'),
        meta: {
            title: '选择理疗师',
            login: true
        }
    },
    {
        path: '/tech/datail',
        name: 'TechDetail',
        component: () => import('../views/techDetail/index.vue'),
        meta: {
            title: '理疗师详情',
            login: true
        }
    },
    {
        path: '/order/datail',
        name: 'OrderDetail',
        component: () => import('../views/orderDetail/index.vue'),
        meta: {
            title: '订单详情',
            login: true
        }
    },
    {
        path: '/placeOrder',
        name: 'PlaceOrder',
        component: () => import('../views/placeOrder/index.vue'),
        meta: {
            title: '下单',
            login: true,
            keepAlive: true
        }
    },
    // 申请技师
    {
        path: '/joinTech',
        name: 'JoinTech',
        component: () => import('../views/joinTech/index.vue'),
        meta: {
            title: '理疗师入驻申请',
            login: false
        }
    },
    {
        path: '/collectList',
        name: 'CollectList',
        component: () => import('../views/collectList/index.vue'),
        meta: {
            title: '我的收藏',
            login: true
        }
    },
    {
        path: '/couponList',
        name: 'CouponList',
        component: () => import('../views/couponList/index.vue'),
        meta: {
            title: '我的优惠劵',
            login: true
        }
    },
    {
        path: '/recharge',
        name: 'Recharge',
        component: () => import('../views/recharge/index.vue')
    },
    {
        path: '/address',
        name: 'Address',
        component: () => import('../views/address/index.vue'),
        meta: {
            title: '地址管理',
            login: true
        }
    },
    {
        path: '/address/edit',
        name: 'AddressEdit',
        component: () => import('../views/address/edit.vue'),
        meta: {
            title: '编辑地址',
            login: true
        }
    },
     // 登录
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/login/index.vue'),
        meta: {
            title: '登录',
            login: false
        }
    },
    // 404页面
    {
        path: "/:pathMatch(.*)*",
        name: "404",
        component: () => import('../views/404/index.vue')
    },
    // 重定向
    {
        path: "/redirect",
        component: () => import("../views/redirect/index.vue")
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

router.beforeEach((to: any, _old, next) => {
    if (to.meta.title) {
        document.title = to.meta.title
    }
    if (to.meta.login) {
        if (localStorage.getItem('token')) {
            next()
        } else {
            next('/login')
            // next()
        }
    } else {
        next()
    }
})

export default router